﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using IBS.ServiceContracts;

namespace IBS.MockDac
{
    public class AdmissionServiceMock : IAdmissionService
    {
        private List<MembershipUser> users = new List<MembershipUser>();
        private Dictionary<string, IList<string>> userInRoles = new Dictionary<string, IList<string>>();


        public AdmissionServiceMock()
        {
            CreateUsers();
        }

        private void CreateUsers()
        {
            CreateUser("guest", "123", "mail@gmail.com");
            CreateUser("userWithCodeCard", "123", "userWithCodeCard@gmail.com");
            CreateUser("userWithoutCodeCard", "123", "userWithoutCodeCard@gmail.com");
        }

        #region IAdmissionService

        public bool UserWithSameNameExists(string login)
        {
            return users.Count(u => u.UserName == login) > 0;
        }

        public bool UserWithSameEmailExists(string email)
        {
            return users.Count(u => u.Email == email) > 0;
        }

        public MembershipUser CreateUser(string userName, string password, string email)
        {
            if (users.Count(u=>u.UserName == userName || u.Email == email) > 0)
                return null;

            //Note: DefaultMembershipProvider have to be in config file (section providers)
            var user = new MembershipUser("DefaultMembershipProvider", userName, "My", email, "123", "123", true, false, DateTime.Now, DateTime.Now, DateTime.Now,
                DateTime.Now, DateTime.Now);


            users.Add(user);
            return user;
        }
        public IList<MembershipUser> GetAll()
        {
            return users;
        }

    
        
        public void AddUserToRole(string userName, string role)
        {
            if (!userInRoles.ContainsKey(userName))
                userInRoles[userName] = new List<string>();

            if (!userInRoles[userName].Contains(role))
                userInRoles[userName].Add(role);
        }

        public IList<string> GetUserRoles(string userName)
        {
            return  userInRoles[userName];
        }
        #endregion
    }
}
